<template>
	<view style="padding: 5%;" class="container">
		<u-sticky offset-top="10" zIndex="999" bgColor="#fff" style="width: 100%;">
			<view>
				<uni-forms ref="formData" :modelValue="formData" label-width="120">
					<uni-forms-item label="铜垛条码" style="background-color: #fff;">
						<uni-easyinput type="text" maxlength="25" :focus="focus" @input="onKeyInput"
							v-model="formData.barcode" placeholder="请扫描铜垛条码" ref='midInput' />
					</uni-forms-item>
				</uni-forms>
			</view>
		</u-sticky>
		<uni-list class="header">
			<!-- to 属性携带参数跳转详情页面，当前只为参考 -->
			<uni-list-item direction="column" v-for="item in listData" :key="item.container_barcode">
				<!-- 通过header插槽定义列表的标题 -->
				<template v-slot:header>
					<view class="uni-title">唯一值：{{item.container_barcode}}
						<uni-icons type="trash" size="22" style="position: absolute;right: 5%;"
							@click="deleteList(item.container_sn)">
						</uni-icons>
					</view>
				</template>
				<!-- 通过body插槽定义列表内容显示 -->
				<template v-slot:body>
					<view class="uni-list-box">
						<view class="uni-content">
							<view class="uni-title-sub uni-ellipsis-2">铜垛条码：{{item.container_sn}}</view>
							<view class="uni-note">批次号：{{item.container_batch + ' 重量：'+item.container_qty}}</view>
						</view>
					</view>
				</template>
				<u-gap height="80" bgColor="#bbb"></u-gap>
			</uni-list-item>
		</uni-list>
		<u-sticky offset-top="80%" zIndex="999" class="footer">
			<view class="uni-content">
				<view class="uni-note" style="font-size: 12px;">
					铜垛总数：{{totalNum + '    总重量：'+totalWeigth + '    订单需求重量：'+totalOutWeigth}}</view>
			</view>
			<button @click="submit" type="primary" size="default" style="margin-top: 2%;">出库确认</button>
		</u-sticky>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					outID: "",
					barcode: '',
				},
				focus: true,
				listData: [],
				barcodes: [],
				totalNum: "",
				totalWeigth: 0,
				totalOutWeigth: ""
			}
		},
		onLoad(res) {
			// this.formData.status=res.status
			this.formData.outID = res.outID
			this.totalOutWeigth = res.totalOutWeigth
		},
		methods: {
			submit() {
				this.$u.post('/api/Dt_container_plane/OutboundTaskCompleted', {
					Extra: this.formData.outID,
					DelKeys: this.barcodes
				}).then(resdt => {
					if (resdt.status) {
						this.$refs.uToast.show({
							title: "所选铜垛出库成功",
							type: "success"
						})
						let pages = getCurrentPages(); //获取所有页面栈实例列表
						// let nowPage = pages[pages.length - 1]; //当前页页面实例
						let prevPage = pages[pages.length - 2]; //上一页页面实例 
						// prevPage.data.refreshIfNeeded = true;
						prevPage.refreshIfNeeded = true;
						// prevPage.$vm.data.forEach(datares => {
						// 	debugger;
						// 	ddatares.refreshIfNeeded = true
						// }); //修改上一页data里面的地址
						setTimeout(() => {
							uni.navigateBack({
								delta: 1 //返回层数，2则上上页
							});
						}, 1300)
					} else {
						this.$refs.uToast.show({
							title: resdt.message,
							type: "error"
						})
					}
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.message,
						type: "error"
					})
				})
			},
			onKeyInput: function(event) {
				if (event.length == 25) {
					
					
					let mydata = [{
						container_barcode: "20" + event.substr(7, 6) + "-" + event.substr(14, 3) + "" + event
							.substr(17, 3) + "" + event.substr(5, 2) + "",
						container_sn: event,
						container_batch: "20" + event.substr(7, 2) + "-" + event.substr(14, 3) + "",
						container_qty: "" + event.substr(20, 4) + "." + event.substr(24) + ""
					}]
					// this.listData.push(data)
					let num = 0;
					for (let i = 0; i < this.listData.length; i++) {
						if (this.listData[i].container_barcode == mydata[0].container_barcode) {
							++num
							break;
						}
					}
					if (num > 0) {
						this.$refs.uToast.show({
							title: "扫描重复",
							type: "error"
						});
						this.$nextTick(() => {
							this.formData.barcode = "";
							this.focus = true;
						})
					} else {
						this.listData = [...this.listData, ...mydata]
						this.totalNum = this.listData.length;
						this.totalWeigth += parseInt(mydata[0].container_qty);
						// debugger;
						this.$nextTick(() => {
							this.formData.barcode = "";
							this.focus = true;
						})
						this.barcodes.push(event);
					}
				}
			},
			deleteList(res) {
				this.listData.map((item, index) => {
					if (item.container_sn == res) {
						this.listData.splice(index, 1);
						this.totalNum -= 1;
						this.totalWeigth -= parseInt(item.container_qty)
					}
				})
				this.barcodes.map((item, index) => {
					let container_sn = item
					if (container_sn == res) {
						this.barcodes.splice(index, 1);
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '@/common/uni-ui.scss';

	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #efeff4;
		min-height: 100%;
		height: auto;
	}

	.tips {
		color: #67c23a;
		font-size: 14px;
		line-height: 40px;
		text-align: center;
		background-color: #f0f9eb;
		height: 0;
		opacity: 0;
		transform: translateY(-100%);
		transition: all 0.3s;
	}

	.tips-ani {
		transform: translateY(0);
		height: 40px;
		opacity: 1;
	}

	.content {
		width: 100%;
		display: flex;
	}

	.list-picture {
		width: 100%;
		height: 145px;
	}

	.thumb-image {
		width: 100%;
		height: 100%;
	}

	.ellipsis {
		display: flex;
		overflow: hidden;
	}

	.uni-ellipsis-1 {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.uni-ellipsis-2 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background: #f1f3f4;

		.header {
			padding-bottom: 80px;
			background-color: #f1f3f4;
		}

		// 使用
		.footer {
			width: 100%;
			text-align: center;
			position: fixed;
			/*1、【relative】相对定位；2、【absolute】绝对定位；3、【fixed】固定定位；4、【static】默认值；5、【sticky】粘性定位。*/
			left: 0px;
			/*定位：离页面左边0px的位置*/
			bottom: 0px;
			/*定位：离页面底部0px的位置*/
		}
	}
</style>